import Mock from 'mockjs';

// 构建原始列表数据
let listData = Mock.mock({
  "综合|5": [{
    id: "@id",
    name: "@ctitle(2,4)",
    image: "@image(100x100,@color,@city)",
    delete: false
  }],
  "贴吧|7": [{
    id: "@id",
    name: "@ctitle(2,4)",
    image: "@image(100x100,@color,@city)",
    delete: false
  }],
  "购物|9": [{
    id: "@id",
    name: "@ctitle(2,4)",
    image: "@image(100x100,@color,@city)",
    delete: false
  }],
  "游戏|3": [{
    id: "@id",
    name: "@ctitle(2,4)",
    image: "@image(100x100,@color,@city)",
    delete: false
  }]
});


// 启动get请求的api /api/list 
Mock.mock('/api/list', 'get', listData);

// 启动 delete请求的api  /api/delete 通过传入的参数删除某条数据
// 第三个参数是一个回调函数 通过其参数option可以获取客户端传入的数据
Mock.mock('/api/delete', 'post', (options) => {
  console.log('options', options);

  // 获取要删除的id
  let {
    id
  } = JSON.parse(options.body);



  // 对原始数据进行遍历 ，删除id对应的那条税局
  Object.keys(listData).forEach((type) => {
    listData[type].forEach((item, i) => {
      // 将id强制转换成字符串以后比较 防止一个id是字符串一个是数字导致数据无法匹配
      if (item.id + '' === id + '') {
        item.delete = true;
        listData[type].splice(i, 1);
      }
    })
  })


  return {
    success: 1,
    message: '数据删除成功',
    data: listData // 将删除后处理过的数据返回
  }
})